﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eDepotSystem
{
    public class Depot
    {
        private List<WorkSchedule> workSchedules;
        //private List<Driver> drivers;
        private List<Vehicle> vehicles;
        private Driver currentDriver = null;
        private string depotName;

        public Depot(
            string depotName
        ){
            this.depotName = depotName;
        }
        public string GetName()
        {
            return depotName;
        }
        public bool LogOn(string userName, string password)
        {
            if (UserBase.Authenticate(userName, password)){
                currentDriver = UserBase.GetDriver(userName);
                return true;
            }
            return false;
        }

        public void LogOff()
        {
            currentDriver = null;
        }

        public Vehicle LocateVehicle(string regNumber)
        {
            Vehicle v;
            if ((v = vehicles.Find(d => d.GetRegNumber() == regNumber)) != null)
            {
                return v;
            };
            return null;
            //throw new NotImplementedException();
        }

        public Driver LocateDriver(string userName)
        {
            Driver d = UserBase.GetDriver(userName);
            /*if ((d != null) && (d.GetDepotName() == this.depotName))
            {
                return d;
            }*/

            return ((d != null) && (d.GetDepotName() == this.depotName)) ? d : null;
        }


        public void SetupWorkSchedule(string client, DateTime sdate, DateTime edate, Vehicle vehicle, Driver driver)
        {
            workSchedules.Add(new WorkSchedule(client, sdate, edate, vehicle, driver));
        }

        public List<WorkSchedule> GetSchedules()
        {
            return workSchedules;
        }
        public List<WorkSchedule> GetSchedulesByVehicle(Vehicle vehicle)
        {
            List<WorkSchedule> outp = new List<WorkSchedule>();
            foreach (WorkSchedule schedule in workSchedules)
            {
                if (schedule.GetVehicle() == vehicle)
                {
                    outp.Add(schedule);
                }
            }
            return outp;
        }
        public List<WorkSchedule> GetSchedulesByDriver(Driver driver)
        {
            List<WorkSchedule> outp = new List<WorkSchedule>();
            foreach (WorkSchedule schedule in workSchedules)
            {
                if (schedule.GetDriver() == driver)
                {
                    outp.Add(schedule);
                }
            }
            return outp;
        }

        public bool IsDriverAvailable(Driver driver)
        {
            bool outp = true;
            foreach (WorkSchedule schedule in GetSchedulesByDriver(driver))
            {
                if (schedule.IsCurrent())
                {
                    outp = false;
                }
            }
            return outp;
        }

        public bool IsVehicleAvailable(Vehicle vehicle)
        {
            bool outp = true;
            foreach (WorkSchedule schedule in GetSchedulesByVehicle(vehicle))
            {
                if (schedule.IsCurrent())
                {
                    outp = false;
                }
            }
            return outp;
        }
        public void AddVehicle(Vehicle vehicle)
        {
            vehicles.Add(vehicle);
        }
        public void RemoveVehicle(Vehicle vehicle)
        {
            vehicles.Remove(vehicle);
        }
        public void TransferVehicle(Vehicle vehicle, Depot targetDepot)
        {
            targetDepot.AddVehicle(vehicle);
            RemoveVehicle(vehicle);
        }
    }
}
